package cn.initcap.algorithm.leetcode;

import java.util.concurrent.LinkedBlockingDeque;

/**
 * 单调队列
 * @author initcap
 * @date Created in 2020/5/13 08:47.
 */
public class HumdrumQueue {

    private LinkedBlockingDeque<Integer> linkedBlockingDeque;

    public HumdrumQueue() {
        linkedBlockingDeque = new LinkedBlockingDeque<>();
    }

    public HumdrumQueue(int capacity) {
        linkedBlockingDeque = new LinkedBlockingDeque<>(capacity);
    }

    public void push(int n) {
        while (!linkedBlockingDeque.isEmpty() && linkedBlockingDeque.peekFirst() < n) {
            linkedBlockingDeque.pollFirst();
        }
        linkedBlockingDeque.addFirst(n);
    }

    public int max() {
        return linkedBlockingDeque.peekLast();
    }

    public void pop(int n) {
        if (!linkedBlockingDeque.isEmpty() && linkedBlockingDeque.peekLast() == n) {
            linkedBlockingDeque.pollLast();
        }
    }

}
